package org.wang.generalcode.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.sys.pojo.entity.Dict;
import org.wang.generalcode.modules.sys.pojo.entity.DictItem;
import org.wang.generalcode.modules.sys.pojo.model.Option;
import org.wang.generalcode.modules.sys.pojo.params.DictPageParams;
import org.wang.generalcode.modules.sys.pojo.vo.DictItemVO;
import org.wang.generalcode.modules.sys.pojo.vo.DictVO;
import org.wang.generalcode.modules.sys.service.IDictItemService;
import org.wang.generalcode.modules.sys.service.IDictService;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典类型表 控制器
 *
 * @author wz
 * @since 2024-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
@Schema(description = "字典类型表", name = "字典类型表接口")
public class DictController {

	private final IDictService dictService;

	private final IDictItemService dictItemService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Schema(description = "详情", example = "传入dict")
	public Result<DictVO> detail(@Parameter(name = "主键集合", required = true) @RequestParam String id) {
		Dict detail = dictService.getById(id);
		DictVO detailVO = BeanUtil.copyProperties(detail, DictVO.class);
		List<DictItem> list = dictItemService.list(Wrappers.<DictItem>lambdaQuery().eq(DictItem::getDictId, id));
		List<DictItemVO> dictItemVOS = BeanUtil.copyToList(list, DictItemVO.class);
		detailVO.setDictItems(dictItemVOS);
		return Result.success(detailVO);
	}

	/**
	 * 自定义分页 字典类型表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Schema(description = "分页", example = "传入dict")
	public Result<IPage<DictVO>> page(DictPageParams dict, Query query) {
		IPage<DictVO> pages = dictService.selectDictPage(Condition.getPage(query), dict);
		return Result.success(pages);
	}

	/**
	 * 新增 字典类型表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Schema(description = "新增", example = "传入dict")
	public Result save(@Valid @RequestBody DictVO dictVO) {
		Dict dict = new Dict();
		List<DictItem> dictItems = new ArrayList<>();
		BeanUtil.copyProperties(dictVO, dict, true);
		dict.setCreateTime(LocalDateTime.now());
		boolean result = dictService.save(dict);
		// 保存字典项
		if (result) {
			List<DictItemVO> dictFormDictItems = dictVO.getDictItems();
			dictItems = BeanUtil.copyToList(dictFormDictItems, DictItem.class);
			dictItems.forEach(dictItem -> {
				dictItem.setDictId(dict.getId());
				dictItem.setCreateTime(DateUtil.date());
				dictItem.setUpdateTime(DateUtil.date());
			});
		}
		return Result.judge(dictItemService.saveBatch(dictItems));
	}

	/**
	 * 修改 字典类型表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Schema(description = "修改", example = "传入dict")
	public Result update(@Valid @RequestBody DictVO dictVO) {
		Dict dict = new Dict();
		List<DictItem> dictItems = new ArrayList<>();
		BeanUtil.copyProperties(dictVO, dict, true);
		dict.setUpdateTime(LocalDateTime.now());
		boolean result = dictService.updateById(dict);
		if (result) {
			List<DictItemVO> dictFormDictItems = dictVO.getDictItems();
			dictItems = BeanUtil.copyToList(dictFormDictItems, DictItem.class);
			dictItems.forEach(dictItem -> {
				dictItem.setDictId(dict.getId());
				dictItem.setCreateTime(DateUtil.date());
				dictItem.setUpdateTime(DateUtil.date());
			});
		}
		return Result.judge(dictItemService.saveOrUpdateBatch(dictItems));
	}


	/**
	 * 删除 字典类型表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@Schema(description = "删除", example = "传入ids")
	public Result remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return Result.judge(dictService.removeByIds(Func.toLongList(ids)) && dictItemService.remove(Wrappers.<DictItem>lambdaQuery().in(DictItem::getDictId, Func.toLongList(ids))));
	}

	/**
	 * 字典数据项列表
	 */
	@GetMapping("/options")
	@ApiOperationSupport(order = 9)
	@Schema(description = "字典数据项列表", example = "传入dict")
	public Result<List<Option<Long>>> getDictOptions(@Parameter(description = "字典编码", required = true) @RequestParam String code) {
		List<Option<Long>> options = dictService.listDictItemsByCode(code);
		return Result.success(options);
	}


}
